<?php
/**
 * Created by 绵阳人维网络科技有限公司.
 * User: 罗东华
 * Date: 2016/5/13
 * Time: 9:36
 */
namespace Admin\Controller;
use Libs\Category;
class CategoryController extends AdminBase{

    public function index(){

        if(IS_AJAX){

            $listorders=I('post.listorders');
            foreach($listorders as $k=>$v){
                M('Category')->where(array('catid'=>$k))->save(array('listorder'=>$v));

            }
            S('CATEGORY_S',null);//清除缓存

            $res['status']='y';$this->ajaxReturn($res);


        }

        $Category=S('CATEGORY_S');
        if(!$Category){
            $catelist=M('Category')->order('listorder ASC')->select();
            $New_Category=new Category($catelist);
            //查询模型
            $Molde=M('model');
            $models=$Molde->getField('modelid,name');
            $New_Category->data=$models;
            $Category=$New_Category->lists_str();
            S('CATEGORY_S',$Category);
        }


        $this->assign('Category',$Category);
        $this->display();
    }

    //添加栏目
    public function edit(){

        $pid=I('get.pid',0,'int');
        $catid=I('get.catid',0,'int');

        if(IS_POST){

            S('CATEGORY_S',null);
            S('CATEGORY_X',null);
            $data=I();
            if(!$data['urltype']){

                $data['url']= urlconfig($catid);
            }
            $data['setting']=serialize($data['setting']);
            if($catid){
                $this->ThisReturn(DB_edit('category','',$data,'save',array('catid'=>$catid)));
            }else{
                $this->ThisReturn(DB_edit('category','',$data));
            }
        }

        if($catid){
            $cates=M('Category')->where(array('catid'=>$catid))->find();
            $pid=$cates['parentid'];
            $cates['setting']=unserialize($cates['setting']);
            $this->assign('cates',$cates);
        }

        //查询模型
        $Molde=M('model');
        $models=$Molde->field('modelid,name')->select();

        $Category=S('CATEGORY_X');
        if(!$Category){
            $catelist=M('Category')->order('listorder ASC')->select();
            $New_Category=new Category($catelist);
            $Category=$New_Category->lists_str(1);
            S('CATEGORY_X',$Category);
        }


        $this->assign('Category',$Category);
        $this->assign('models',$models);
        $this->assign('pid',$pid);

        $this->display();
    }

    //删除栏目
    public function del(){
        $catid=I('get.catid',0,'int');
        if(!$catid){

            $this->ajaxReturn(validreturn(false,'','栏目id错误'));
        }
        $where['catid']=$catid;
        $count=M('Category')->where(array('parentid'=>$catid))->count();
        if($count>0){
            $this->ajaxReturn(validreturn(false,'','有下级栏目'));
        }
        S('CATEGORY_S',null);
        $this->ajaxReturn(Db_del('Category',$where));



    }

    //添加单页
    public function page_edit(){

        $this->display();
    }

    //外链栏目
    public function link_edit(){

        $this->display();
    }
    //更新栏目地址
    public function updateurl(){

        if(IS_AJAX){
            $num=100;//每轮更新条数
            $page=I('post.num',0,'int');
            $category=M('category');
            $where['status']=0;
            $where['urltype']=0;
            $count=$category->where($where)->count();
            $pagex=$page+1;
            $times=ceil($count/$num);
            if($pagex>$times){
                $data['page']=-1;
                $this->ajaxReturn($data);exit;
            }

            $list=$category->where($where)->limit($page*$num,$num)->select();
            //$a=$category->_sql();
            foreach($list as $v){

                $url=urlconfig($v['catid']);
                $w['catid']=$v['catid'];
                $w['urltype']=0;
                $category->where($w)->save(array('url'=>$url));


            }

            $data['num']=$num;
            $data['page']=$pagex;
            $data['count']=$count;
            //$data['a']=$a;

            $this->ajaxReturn($data);

        }




    }



}